package com.netty.server.handler;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netty.common.protocol.RpcRequest;
import com.netty.server.Server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ServerHandler extends SimpleChannelInboundHandler<RpcRequest> {

	private static final Logger logger = LoggerFactory.getLogger(ServerHandler.class);
	private final Map<String, Object> handlerMap;
	public ServerHandler(Map<String, Object> handlerMap){
		this.handlerMap = handlerMap;
	}
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, RpcRequest request)
			throws Exception {
		Task task = new Task(ctx, request,handlerMap);
		Server.submit(task);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		logger.error("server caught exception", cause);
		ctx.close();
	}

}
